package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyStatisVO;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.domain.Visitors;
import cn.wolfcode.wolf2w.redis.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.IVisitorRedisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.service.IVisitorsService;
import cn.wolfcode.wolf2w.service.impl.UserInfoServiceImpl;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.RedisKeys;
import cn.wolfcode.wolf2w.util.VisitorVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class VisitorRedisServiceImpl implements IVisitorRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IVisitorsService visitorsService;


    @Override
    public void incrVisitor(String ownerId, String uid) {
        //拼接key,统计vo对象
        String key = RedisKeys.VISITOR_USER_VO.join(ownerId);
        //判断当前进入的访客key是否存在，如果存在，则将总访客数+1，并更新vo，记录进入的访客用户id
        if(redisTemplate.hasKey(key)){
            String vostr = redisTemplate.opsForValue().get(key);
            VisitorVo vo = JSON.parseObject(vostr, VisitorVo.class);
            vo.setOwnerId(ownerId);
            vo.setTotalView(vo.getTotalView()+1);
            List<String> visitorId = vo.getVisitorIds();
            visitorId.add(uid);
            redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
            //如果不存在，则初始化vo对象，将mongodb数据赋值到vo中，将访客数+1，缓存入redis中
        }else {
        VisitorVo vo = new VisitorVo();
        Visitors visitor = visitorsService.findByOwnerId(ownerId);
        if(visitor==null){
            Visitors visitors = new Visitors();
            visitors.setOwnerId(ownerId);
            vo.setTotalView(1);
            visitorsService.save(visitors);
            BeanUtils.copyProperties(visitors,vo);
        }else {
            visitor.setOwnerId(ownerId);
            visitor.setTotalView(visitor.getTodayView()+1);
            BeanUtils.copyProperties(visitor,vo);
        }

        redisTemplate.opsForValue().set(key, JSON.toJSONString(vo));
        }
    }

    @Override
    public VisitorVo queryVisitor(String ownerId) {
        //拼接key
        String key = RedisKeys.VISITOR_USER_VO.join(ownerId);
        VisitorVo vo = null;
        if(redisTemplate.hasKey(key)){
            String vostr = redisTemplate.opsForValue().get(key);
            vo = JSON.parseObject(vostr, VisitorVo.class);
        }else {
           vo = new VisitorVo();
            Visitors visitor = visitorsService.findByOwnerId(ownerId);
            BeanUtils.copyProperties(visitor,vo);
        }
        return vo;
    }

    @Override
    public void incrTodayView(String id, String userInfoId) {
        String key = RedisKeys.VISITOR_TODAYVIEW.join(id);
        Date endDate = DateUtil.getEndDate(new Date());
        long dateBetween = DateUtil.getDateBetween(new Date(), endDate);
        Long todayView = 0L;
        if(redisTemplate.hasKey(key)){
            String value = redisTemplate.opsForValue().get(key);
           todayView = JSON.parseObject(value, Long.class);
            todayView = todayView+1;
        }else{
             todayView =todayView+1 ;
        }
        redisTemplate.opsForValue().set(key, JSON.toJSONString(todayView),dateBetween,TimeUnit.SECONDS);
    }

    @Override
    public Long queryTodayView(String id) {
        String key = RedisKeys.VISITOR_TODAYVIEW.join(id);
        Long todayView = 0L;
        if (redisTemplate.hasKey(key)){
            String todayViewStr = redisTemplate.opsForValue().get(key);
            todayView = JSON.parseObject(todayViewStr, Long.class);
        }else {
            Date endDate = DateUtil.getEndDate(new Date());
            long dateBetween = DateUtil.getDateBetween(new Date(), endDate);
            redisTemplate.opsForValue().set(key,JSON.toJSONString(todayView),dateBetween,TimeUnit.SECONDS);
        }
        return todayView;
    }

    @Override
    public List<VisitorVo> queryVisitorRedisByPattern(String patten) {
          //拼接通配符
        String keypatten = RedisKeys.VISITOR_USER_VO.join(patten);
        Set<String> keys = redisTemplate.keys(keypatten);
        List<VisitorVo> list = new ArrayList<>();
        for (String key : keys) {
            String vostr = redisTemplate.opsForValue().get(key);
            VisitorVo visitorVo = JSON.parseObject(vostr, VisitorVo.class);
            list.add(visitorVo);
        }
        return list;
    }
}
